﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System.Linq;

namespace FarmSmartSleep.Organ.API.Application.Commands.DepartmentCommands
{
    public class DepartSearchHandler : IRequestHandler<DepartSearchCommand, ResultPage<DepartTypeDtos>>
    {
        private readonly IBaseRepository<Departments> departbaseRepository;
        private readonly IBaseRepository<DepartmentType> departmentTypeRepository;
        

        public DepartSearchHandler(IBaseRepository<Departments> departbaseRepository, IBaseRepository<DepartmentType> departmentTypeRepository)
        {
            this.departbaseRepository = departbaseRepository;
            this.departmentTypeRepository = departmentTypeRepository;
        }
        /// <summary>
        /// 部门查询
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultPage<DepartTypeDtos>> Handle(DepartSearchCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<DepartTypeDtos>() { Code = (int)CodeEnum.search};
            var list= (from a in departbaseRepository.GetAll()
                      join b in departmentTypeRepository.GetAll() on a.DepartmentTypeId equals b.DepartmentTypeId
                      select new DepartTypeDtos()
                      {
                          DepartmentId=a.DepartmentId,
                          DepartmentName=a.DepartmentName,
                          DepartmentStop=a.DepartmentStop,
                          DepartmentTypeId=a.DepartmentTypeId,
                          ProvinceId = a.ProvinceId,
                          CityId = a.CityId,
                          DirectSuperior = a.DirectSuperior,
                          DirectSuperiorName = a.DirectSuperiorName,
                          InChargeHead = a.InChargeHead,
                          Treasurer = a.Treasurer,
                          ViewablePersonnel = a.ViewablePersonnel,
                          FunctionalSystemId = a.FunctionalSystemId,
                          DepartmentTypeName=b.DepartmentTypeName
                      }).ToList();

            if (!string.IsNullOrEmpty(request.DepartmentName))
            {
                list=list.Where(x=>x.DepartmentName.Contains(request.DepartmentName)).ToList();
            }
            if (request.DepartmentTypeId>0)
            {
                list = list.Where(x => x.DepartmentTypeId == request.DepartmentTypeId).ToList();
            }
            result.totalCount = list.Count;
            result.totalPage = (int)Math.Ceiling(result.totalCount * 1.0 / request.Psize);
            list = list.OrderBy(x=>x.DepartmentId).Skip((request.Pindex-1)*request.Psize).Take(request.Psize).ToList();
            result.Data = list;
            return result;
        }
    }
}
